<?php
/*
 * @ https://EasyToYou.eu - IonCube v11 Decoder Online
 * @ PHP 7.2 & 7.3
 * @ Decoder version: 1.1.6
 * @ Release: 10/08/2022
 */

// Decoded file for php version 71.
namespace app\massage\controller;

class AdminService extends \app\AdminRest
{
    protected $model;
    protected $goods_sh;
    protected $goods_sh_list;
    public function __construct(\think\App $app)
    {
        parent::__construct($app);
        $this->model = new \app\massage\model\Service();
    }
    public function serviceList()
    {
        $this->model->where(["type" => 1, "admin_id" => 11])->update(["admin_id" => 0]);
        $input = $this->_param;
        $admin_model = new \app\massage\model\Admin();
        $dis[] = ["uniacid", "=", $this->_uniacid];
        $type = !empty($input["type"]) ? $input["type"] : 1;
        if(!empty($input["status"])) {
            $dis[] = ["status", "=", $input["status"]];
        } else {
            $dis[] = ["status", ">", -1];
        }
        $where = [];
        if(!empty($input["name"])) {
            $where[] = ["title", "like", "%" . $input["name"] . "%"];
            $admin_id = $admin_model->where("agent_name", "like", "%" . $input["name"] . "%")->column("id");
            if($input["name"] == "平台") {
                $admin_id[] = 0;
            }
            $where[] = ["admin_id", "in", $admin_id];
        }
        if($type == 1) {
            $top = "top desc,id desc";
            $dis[] = ["check_status", "=", 2];
        } else {
            $top = "id desc";
            $dis[] = ["type", "=", $type];
        }
        $auth = \longbingcore\permissions\AdminMenu::getAuthList((int) $this->_uniacid, ["store"]);
        if(empty($auth["store"])) {
            $dis[] = ["is_door", "=", 1];
        }
        if(!empty($input["check_status"])) {
            $dis[] = ["check_status", "=", $input["check_status"]];
        }
        if($this->_user["is_admin"] == 0) {
            $dis[] = ["admin_id", "in", [$this->_user["admin_id"], 0]];
        }
        if(!empty($input["coupon_admin_id"])) {
            $dis[] = ["admin_id", "in", [$input["coupon_admin_id"], 0]];
        }
        if(!empty($input["admin_id"])) {
            $dis[] = ["admin_id", "=", $input["admin_id"]];
        }
        if(!empty($input["industry_type"])) {
            $dis[] = ["industry_type", "=", $input["industry_type"]];
        }
        if(!empty($input["start_time"]) && !empty($input["end_time"])) {
            $dis[] = ["create_time", "between", $input["start_time"] . "," . $input["end_time"]];
        }
        $is_add = !empty($input["is_add"]) ? $input["is_add"] : 0;
        $dis[] = ["is_add", "=", $is_add];
        $data = $this->model->dataList($dis, $input["limit"], $top, $where);
        if(!empty($data["data"])) {
            $city_model = new \app\massage\model\City();
            $industry_model = new \app\industrytype\model\Type();
            foreach ($data["data"] as &$v) {
                $v["industry_title"] = $industry_model->where(["id" => $v["industry_type"], "status" => 1])->value("title");
                if(!empty($v["admin_id"])) {
                    $v["admin_name"] = $admin_model->where(["id" => $v["admin_id"]])->value("agent_name");
                    $city_id = $admin_model->where(["id" => $v["admin_id"]])->value("city_id");
                    $v["admin_city"] = $city_model->where(["id" => $city_id])->value("title");
                } else {
                    $v["admin_name"] = "平台";
                }
            }
        }
        $list = ["all", "ing", "pass", "nopass"];
        foreach ($list as $k => $value) {
            $dis = ["uniacid" => $this->_uniacid, "type" => 2];
            if($this->_user["is_admin"] == 0) {
                $dis["admin_id"] = $this->_user["admin_id"];
            }
            if(!empty($k)) {
                $dis["check_status"] = $k;
            }
            $data[$value] = $this->model->where($dis)->where("status", ">", -1)->count();
        }
        return $this->success($data);
    }
    public function serviceInfo()
    {
        $input = $this->_param;
        $dis = ["id" => $input["id"]];
        $admin_id = $this->_user["is_admin"] == 0 ? $this->_user["admin_id"] : 0;
        $data = $this->model->dataInfo($dis, $admin_id);
        $store_model = new \app\massage\model\StoreService();
        $data["store"] = $store_model->getServiceStore($input["id"]);
        $cate_model = new \app\massage\model\CateConnect();
        $data["cate_id"] = $cate_model->getServiceCate($input["id"]);
        $member_service_model = new \app\member\model\Service();
        $data["service_level"] = $member_service_model->getMemberService($input["id"]);
        $cate_model = new \app\massage\model\CateList();
        $data["cate_name"] = $cate_model->where("id", "in", $data["cate_id"])->column("title");
        $data["cate_name"] = array_values($data["cate_name"]);
        if(!empty($data["admin_id"])) {
            $admin_model = new \app\massage\model\Admin();
            $data["reseller_auth"] = $admin_model->where(["id" => $data["admin_id"]])->value("reseller_auth");
        } else {
            $data["reseller_auth"] = true;
        }
        $position_model = new \app\massage\model\ServicePositionConnect();
        $position = $position_model->positionInfo($data["id"]);
        $data["position"] = array_column($position, "id");
        $data["position_title"] = array_column($position, "title");
        $industry_model = new \app\industrytype\model\Type();
        $data["industry_title"] = $industry_model->where(["id" => $data["industry_type"], "status" => 1])->value("title");
        $guarantee_model = new \app\massage\model\ServiceGuaranteeConnect();
        $guarantee = $guarantee_model->guaranteeInfo($data["id"]);
        $data["guarantee"] = array_column($guarantee, "id");
        $data["guarantee_title"] = array_column($guarantee, "title");
        return $this->success($data);
    }
    public function serviceAdd()
    {
        $input = $this->_input;
        $input["uniacid"] = $this->_uniacid;
        $input["admin_id"] = 0;
        if($this->_user["is_admin"] == 0) {
            $input["type"] = 2;
            $input["check_status"] = 1;
            $input["admin_id"] = $this->_user["admin_id"];
        }
        $res = $this->model->dataAdd($input);
        return $this->success($res, 200, $res);
    }
    public function serviceUpdate()
    {
        $input = $this->_input;
        $input["uniacid"] = $this->_uniacid;
        $dis = ["id" => $input["id"]];
        $info = $this->model->dataInfo($dis);
        if($this->_user["is_admin"] == 0 && isset($input["title"])) {
            $input["check_status"] = 1;
        }
        $res = $this->model->dataUpdate($dis, $input);
        return $this->success($res);
    }
    public function checkStoreGoods()
    {
        $input = $this->_input;
        $dis = ["id" => $input["id"]];
        $data = $this->model->dataInfo($dis);
        if($data["check_status"] != 1) {
            $this->errorMsg("服务已经审核");
        }
        $update = ["check_status" => $input["check_status"], "check_text" => $input["check_text"], "check_time" => time()];
        $res = $this->model->dataUpdate($dis, $update);
        return $this->success($res);
    }
    public function cateList()
    {
        $input = $this->_param;
        if(!empty($input["status"])) {
            $dis[] = ["status", "=", $input["status"]];
        } else {
            $dis[] = ["status", ">", -1];
        }
        $dis[] = ["uniacid", "=", $this->_uniacid];
        if(!empty($input["title"])) {
            $dis[] = ["title", "like", "%" . $input["title"] . "%"];
        }
        $cate_model = new \app\massage\model\CateList();
        $data = $cate_model->dataList($dis, $input["limit"]);
        return $this->success($data);
    }
    public function cateAdd()
    {
        $input = $this->_input;
        $input["uniacid"] = $this->_uniacid;
        $cate_model = new \app\massage\model\CateList();
        $res = $cate_model->dataAdd($input);
        return $this->success($res);
    }
    public function cateUpdate()
    {
        $input = $this->_input;
//        print_r($input);
        $dis = ["id" => $input["id"]];
        $cate_model = new \app\massage\model\CateList();
        if(isset($input["status"]) && in_array($input["status"], [-1])) {
            $connect_model = new \app\massage\model\CateConnect();
            $have = $connect_model->getCateService($input["id"]);
            if(!empty($have)) {
                $this->errorMsg("有服务正在使用该分类");
            }
        }
        $res = $cate_model->dataUpdate($dis, $input);
        return $this->success($res);
    }
    public function cateInfo()
    {
        $input = $this->_param;
        $dis = ["id" => $input["id"]];
        $cate_model = new \app\massage\model\CateList();
        $res = $cate_model->dataInfo($dis);
        return $this->success($res);
    }
    public function cateSelect()
    {
        $input = $this->_param;
        $dis = ["uniacid" => $this->_uniacid, "status" => 1];
        $cate_model = new \app\massage\model\CateList();
        $res = $cate_model->where($dis)->order("top desc,id desc")->select()->toArray();
        return $this->success($res);
    }
    public function positionList()
    {
        $input = $this->_param;
        $model = new \app\massage\model\ServicePositionList();
        $dis[] = ["uniacid", "=", $this->_uniacid];
        $dis[] = ["status", ">", -1];
        if(!empty($input["title"])) {
            $dis[] = ["title", "like", "%" . $input["title"] . "%"];
        }
        $data = $model->dataList($dis, $input["limit"]);
        return $this->success($data);
    }
    public function positionSelect()
    {
        $input = $this->_param;
        $model = new \app\massage\model\ServicePositionList();
        $dis[] = ["uniacid", "=", $this->_uniacid];
        $dis[] = ["status", "=", 1];
        $data = $model->where($dis)->order("top desc,id desc")->select()->toArray();
        return $this->success($data);
    }
    public function positionAdd()
    {
        $input = $this->_input;
        $model = new \app\massage\model\ServicePositionList();
        $input["uniacid"] = $this->_uniacid;
        $data = $model->dataAdd($input);
        return $this->success($data);
    }
    public function positionUpdate()
    {
        $input = $this->_input;
        $model = new \app\massage\model\ServicePositionList();
        $dis = ["id" => $input["id"]];
        $data = $model->dataUpdate($dis, $input);
        return $this->success($data);
    }
    public function positionInfo()
    {
        $input = $this->_param;
        $model = new \app\massage\model\ServicePositionList();
        $dis = ["id" => $input["id"]];
        $data = $model->dataInfo($dis);
        return $this->success($data);
    }
    public function guaranteeList()
    {
        $input = $this->_param;
        $where = [["uniacid", "=", $this->_uniacid], ["status", "=", 1]];
        $data = \app\massage\model\ServiceGuarantee::getList($where, $input["limit"] ?? 10);
        return $this->success($data);
    }
    public function guaranteeAdd()
    {
        $data = $this->request->only(["title", "sub_title", "top"]);
        $data["uniacid"] = $this->_uniacid;
        $res = \app\massage\model\ServiceGuarantee::add($data);
        return $this->success($res);
    }
    public function guaranteeUpdate()
    {
        $data = $this->request->only(["id", "title", "sub_title", "top", "status"]);
        $res = \app\massage\model\ServiceGuarantee::edit(["id" => $data["id"]], $data);
        return $this->success($res);
    }
    public function guaranteeListNoPage()
    {
        $where = [["uniacid", "=", $this->_uniacid], ["status", "=", 1]];
        $data = \app\massage\model\ServiceGuarantee::getListNoPae($where);
        return $this->success($data);
    }
}

?>